const courseService = require('../services/courseService');

class CourseController {
  /**
   * 创建课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async createCourse(ctx, next) {
    try {
      const { title, description, price, displayImage, detailImages } = ctx.request.body;

      const course = await courseService.createCourse(title, description, price, displayImage, detailImages);

      ctx.status = 201;
      ctx.body = course;
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '创建课程失败', error };
    }

    await next();
  }

  /**
   * 获取所有课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async getAllCourses(ctx, next) {
    try {
      const courses = await courseService.getAllCourses();

      ctx.status = 200;
      ctx.body = { courses };
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '获取课程列表失败', error };
    }

    await next();
  }

  /**
   * 根据课程ID获取单个课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async getCourseById(ctx, next) {
    try {
      const { id } = ctx.params;

      const course = await courseService.getCourseById(id);

      if (!course) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = course;
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '获取课程详情失败', error };
    }

    await next();
  }

  /**
   * 更新课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async updateCourse(ctx, next) {
    try {
      const { id } = ctx.params;
      const { title, description, price, displayImage, detailImages } = ctx.request.body;

      const success = await courseService.updateCourse({ id, title, description, price, displayImage, detailImages });

      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '更新课程失败', error };
    }

    await next();
  }

  /**
   * 删除课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async deleteCourse(ctx, next) {
    try {
      const { id } = ctx.params;

      const success = await courseService.deleteCourse(id);

      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '删除课程失败', error };
    }
  
    await next();
  }
  
  /**
   * 上架课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async publishCourse(ctx, next) {
    try {
      const { id } = ctx.params;
  
      const success = await courseService.publishCourse(id);
  
      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '上架课程失败', error };
    }
  
    await next();
  }
  
  /**
   * 下架课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async unpublishCourse(ctx, next) {
    try {
      const { id } = ctx.params;
  
      const success = await courseService.unpublishCourse(id);
  
      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '下架课程失败', error };
    }
  
    await next();
  }
  
  /**
   * 批量删除课程
   * @param {Object} ctx - Koa context
   * @param {Function} next - Koa next middleware function
   */
  async deleteCourses(ctx, next) {
    try {
      const { ids } = ctx.request.body;
  
      const success = await courseService.deleteCourses(ids);
  
      if (!success) {
        ctx.status = 404;
        ctx.body = { message: '找不到指定ID的课程' };
      } else {
        ctx.status = 200;
        ctx.body = { success: true };
      }
    } catch (error) {
      ctx.status = 500;
      ctx.body = { message: '批量删除课程失败', error };
    }
  
    await next();
  }
  
  }
  
  module.exports = new CourseController();
  